//
//  ViewController.m
//  UI09_homework(省市区)
//
//  Created by Hanlingyu on 16/7/26.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "City.h"
#import "Province.h"
#import "Zone.h"
#import "CityViewController.h"

#define kTableViewReuse @"reuse"
@interface ViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate
>
@property (nonatomic, strong) UITableView *provinceTableView;
@property (nonatomic, strong) NSMutableArray *provinceArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationItem.title = @"省份";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.153 green:0.661 blue:1.000 alpha:1.000];

    self.provinceArray = [self handleProvinceArray];
    
    self.provinceTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _provinceTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_provinceTableView];
    
    _provinceTableView.dataSource = self;
    _provinceTableView.delegate = self;
    
    


}

- (NSMutableArray *)handleProvinceArray {
    
    NSString *sourceStr = [NSString stringWithContentsOfFile:@"/Users/ltq/DLS160710/UI/HomeWork/UI09_homework(省市区)/UI09_homework(省市区)/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [sourceStr componentsSeparatedByString:@"\n"];
    
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            NSArray *arr = [area componentsSeparatedByString:@" "];
            Province *pro = [Province zoneWithName:arr.firstObject number:arr.lastObject];
            pro.array = [NSMutableArray array];
            [allProArray addObject:pro];
        } else if (![area hasPrefix:@"    "]) {
            NSString *str = [area substringFromIndex:2];
            NSArray *arr = [str componentsSeparatedByString:@" "];
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:arr.firstObject number:arr.lastObject];
            city.array = [NSMutableArray array];
            [pro.array addObject:city];
        } else {
            NSString *str = [area substringFromIndex:4];
            NSArray *arr = [str componentsSeparatedByString:@" "];
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:arr.firstObject number:arr.lastObject];
            [city.array addObject:zone];
        }
    }
    return allProArray;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _provinceArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewReuse];
    }

    Province *province = _provinceArray[indexPath.row];
    cell.textLabel.text = province.name;
    cell.detailTextLabel.text = province.number;
    return cell;
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CityViewController *cityVC = [[CityViewController alloc] init];
    [self.navigationController pushViewController:cityVC animated:YES];
    Province *pro = _provinceArray[indexPath.row];
    cityVC.province = pro;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
